package ru.dc.object.commands;

/**
<h3>Syntax</h3>
<pre>
 $To: &lt;othernick> From: &lt;nick> $&lt;&lt;nick>> &lt;message>|
 </pre>
<h3>Description</h3>
 <ul>
<li>This command is used to send a private message to a peer.</li>
<li>&lt;othernick> is the peer who should receive the message.</li>
<li>&lt;nick> is the sender's nick (both instances). </li>
<li>&lt;message> is the text of the message (spaces permitted).</li>
<li>The server must pass the message unmodified to client &lt;othernick> which must display the message to the user.</li>
</ul>
*/
public class To extends Command {

    private String sourceNick;
    private String targetNick;
    private String senderNick;
    private String message;

    @Override
    public String generateCommand() {
        return getActualCommandName()+" "+getTargetNick()+" From: "+getSourceNick()+" $<"+getSenderNick()+"> "+getMessage();
    }

    @Override
    public void parseCommand() {

        int startTargetNick = getActualCommandName().length()+1; //" "
        int endTargetNick = getCommand().indexOf(" From: ", startTargetNick);
        setTargetNick(getCommand().substring(startTargetNick, endTargetNick));

        int startSourceNick = endTargetNick + 7;//" From: "
        int endSourceNick = getCommand().indexOf(" $<", startSourceNick);
        setSourceNick(getCommand().substring(startSourceNick, endSourceNick));

        int startSenderNick = endSourceNick+ 3;//" $<"
        int endSenderNick = getCommand().indexOf("> ", startSenderNick);
        setSenderNick(getCommand().substring(startSenderNick, endSenderNick));

        int startMessage = endSenderNick+2;//"> ";
        setMessage(getCommand().substring(startMessage));
    }

    @Override
    public String getActualCommandName() {
        return "$To:";
    }

    public String getSourceNick() {
        return sourceNick;
    }

    public void setSourceNick(String sourceNick) {
        this.sourceNick = sourceNick;
    }

    public String getTargetNick() {
        return targetNick;
    }

    public void setTargetNick(String targetNick) {
        this.targetNick = targetNick;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getSenderNick() {
        return senderNick;
    }

    public void setSenderNick(String senderNick) {
        this.senderNick = senderNick;
    }
}
